﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;


namespace Marmat.Forms
{
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(TopicBar), "TopicBar.ico")]
    public partial class TopicBar : System.Windows.Forms.UserControl, IControl
    {

        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        public TopicBar()
        {
            /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
            base.SizeChanged += TopicBar_SizeChanged;
            // 组件设计器需要此调用。
            this.InitializeComponent();
            this.SetStyle(ControlStyles.ResizeRedraw, true); // 尺寸变化后绘制
            this.SetStyle(ControlStyles.Opaque, false); // 改善绘图质量
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); // 使控件支持透明色
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        private bool DrawHeight = false;
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        protected override void OnPaint(PaintEventArgs e)
        {
            // 绘制背景=============================================================
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            TopicBarRenderer Renderer = this.Renderer ?? new TopicBarRenderer(this);
            Renderer.OnRenderBackground(e);
            int mLeft = this.Padding.Left - this.HorizontalScroll.Value;
            int mWidth = this.VScroll ? this.Width - this.Padding.Left - 17 : this.Width - this.Padding.Left;
            int mHeight = this.Padding.Top + this.Padding.Bottom;
            if (mWidth > 0 && this.PageHeight > 0)
            {
                e.Graphics.SetClip(new Rectangle(mLeft, this.Padding.Top, mWidth - this.Padding.Right + 1, this.Height - this.Padding.Left - this.Padding.Bottom));
                foreach (TopicBarPage Page in this.Pages)
                {
                    if (Page.Visible == true)
                    {
                        int y = this.Padding.Top + mHeight - this.VerticalScroll.Value;
                        Page.Rectangle = new Rectangle(mLeft, y, mWidth - mLeft, this.PageHeight);
                        mHeight += this.PageHeight;
                        if (Page.Rectangle.Top < this.Height) // 绘制面板==================================================
                        {
                            using (var Bmp = new Bitmap(Page.Rectangle.Width, Page.Rectangle.Height))
                            {
                                using (var G_Bmp = Graphics.FromImage(Bmp))
                                {
                                    Renderer.OnRenderPageBackground(new TopicBarPageRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Page = Page });
                                    Renderer.OnRenderPageImage(new TopicBarPageRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Page = Page });
                                    Renderer.OnRenderPageText(new TopicBarPageRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Page = Page });
                                    e.Graphics.DrawImage(Bmp, Page.Rectangle);
                                }
                            }

                            if (Page.ExpandPage == true) // '绘制子项==================================================
                            {
                                if (this.DrawMode == DarwModeEnum.Row)
                                {
                                    foreach (TopicBarItem Item in Page.Items)
                                    {
                                        if (Item.Visible == true)
                                        {
                                            y = this.Padding.Top + mHeight - this.VerticalScroll.Value;
                                            Item.Rectangle = new Rectangle(mLeft, y, mWidth - mLeft, this.ItemHeight);
                                            mHeight += this.ItemHeight;
                                            if (Item.Rectangle.Top < this.Height)
                                            {
                                                using (var Bmp = new Bitmap(Item.Rectangle.Width, Item.Rectangle.Height))
                                                {
                                                    using (var G_Bmp = Graphics.FromImage(Bmp))
                                                    {
                                                        Renderer.OnRenderItemBackground(new TopicBarItemRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Item = Item });
                                                        Renderer.OnRenderItemImage(new TopicBarItemRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Item = Item });
                                                        Renderer.OnRenderItemText(new TopicBarItemRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Item = Item });
                                                        e.Graphics.DrawImage(Bmp, Item.Rectangle);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (this.DrawMode == DarwModeEnum.Col)
                                {
                                    int Item_x = mLeft;
                                    int Col = 0;
                                    foreach (TopicBarItem Item in Page.Items)
                                    {
                                        if (Item.Visible == true)
                                        {
                                            if (Col > 0 && Item_x + this.MaxItemWith > this.Width)
                                            {
                                                Col = 0;
                                                Item_x = mLeft;
                                                mHeight += this.ItemHeight + this.ItemSpacing;
                                            }

                                            Col += 1;
                                            Item.Rectangle = new Rectangle(Item_x, this.Padding.Top + mHeight - this.VerticalScroll.Value, this.MaxItemWith, this.ItemHeight);
                                            Item_x += Item.Rectangle.Width;
                                            if (Item.Rectangle.Top < this.Height)
                                            {
                                                using (var Bmp = new Bitmap(Item.Rectangle.Width, Item.Rectangle.Height))
                                                {
                                                    using (var G_Bmp = Graphics.FromImage(Bmp))
                                                    {
                                                        // e.Graphics.DrawRectangle(Pens.Red, Item.Rectangle)
                                                        // TextRenderer.DrawText(e.Graphics, Item.Text, Me.ItemForeFont, Item.Rectangle, Color.Red, TextFormatFlags.VerticalCenter Or TextFormatFlags.HorizontalCenter)
                                                        Renderer.OnRenderItemBackground(new TopicBarItemRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Item = Item });
                                                        Renderer.OnRenderItemImage(new TopicBarItemRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Item = Item });
                                                        Renderer.OnRenderItemText(new TopicBarItemRendererEventArgs() { m_TopicBar = this, m_Graphics = G_Bmp, m_Item = Item });
                                                        e.Graphics.DrawImage(Bmp, Item.Rectangle);
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    mHeight += this.ItemHeight + this.ItemSpacing;
                                }
                            }

                            mHeight += this.PageSpacing + 1;
                        }
                    }
                }
            }

            if (this.DrawHeight == true && mHeight != this.AutoScrollMinSize.Height)
            {
                this.AutoScrollMinSize = new Size(0, mHeight);
            }

            this.DrawHeight = false;
        }
        // Protected Overrides Sub OnPaint(e As PaintEventArgs)
        // '绘制背景=============================================================
        // Dim Renderer As BI.Win.TopicBarRenderer = IIf(Me.Renderer IsNot Nothing, Me.Renderer, New TopicBarRenderer(Me))
        // Renderer.OnRenderBackground(e)
        // e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        // e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
        // Dim x As Integer = Me.Padding.Left - Me.HorizontalScroll.Value
        // Dim w As Integer = IIf(Me.VScroll, Me.Width - Me.Padding.Left - 17, Me.Width - Me.Padding.Left)
        // Dim h As Integer = Me.Padding.Top + Me.Padding.Bottom
        // Dim Flags As System.Windows.Forms.TextFormatFlags = TextFormatFlags.VerticalCenter Or TextFormatFlags.PreserveGraphicsClipping
        // If w > 0 AndAlso Me.PageHeight > 0 Then
        // Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
        // e.Graphics.SetClip(New Rectangle(x, Me.Padding.Top, w - Padding.Right + 1, Me.Height - Me.Padding.Left - Me.Padding.Bottom))
        // For Each Page As TopicBarPage In Me.Pages
        // If Page.Visible = True Then
        // Dim y As Integer = Me.Padding.Top + h - Me.VerticalScroll.Value
        // Page.Rectangle = New Rectangle(x, y, w - x, Me.PageHeight)
        // Using Bmp As New Bitmap(Page.Rectangle.Width, Page.Rectangle.Height)
        // Using G_Bmp As Graphics = Graphics.FromImage(Bmp)
        // Renderer.OnRenderPageBackground(New BI.Win.TopicBarPageRendererEventArgs())
        // e.Graphics.DrawImage(Bmp, Page.Rectangle)
        // End Using
        // End Using
        // h += Me.PageHeight
        // Dim Points(3) As Point
        // If Page.Rectangle.Top < Me.Height Then
        // '绘制渐变矩形==============================================
        // Points(0) = New Point(x, y)
        // Points(1) = New Point(w, Page.Rectangle.Top)
        // Points(2) = New Point(w, Page.Rectangle.Bottom)
        // Points(3) = New Point(x, Page.Rectangle.Bottom)
        // Using Brushes As New LinearGradientBrush(Page.Rectangle, Me.PageTopColor, Me.PageBottomColor, 90)
        // If Me.SelectedRectangle <> Nothing AndAlso Page.Rectangle = Me.SelectedRectangle Then
        // Brushes.LinearColors = {Me.MouseMovePageTopColor, Me.MouseMovePageBottomColor}
        // End If
        // e.Graphics.FillPolygon(Brushes, Points)
        // End Using
        // '绘制分割线================================================
        // Using Pen As New Pen(Me.PageSplit0Color)
        // e.Graphics.DrawLine(Pen, New Point(Page.Rectangle.X, Page.Rectangle.Top), New Point(w - 1, Page.Rectangle.Top))
        // Pen.Color = Me.PageSplit1Color
        // e.Graphics.DrawLine(Pen, New Point(Page.Rectangle.X, Page.Rectangle.Top + 1), New Point(w - 1, Page.Rectangle.Top + 1))
        // Pen.Color = Me.PageSplit2Color
        // e.Graphics.DrawLine(Pen, New Point(Page.Rectangle.X, Page.Rectangle.Bottom), New Point(w - 1, Page.Rectangle.Bottom))
        // End Using
        // '绘制图标==================================================
        // Dim PageImage As Image, Image_x As Integer, Image_y As Integer
        // If Page.Image IsNot Nothing Then
        // PageImage = Page.Image.GetThumbnailImage(24, 24, myCallback, IntPtr.Zero)
        // Image_x = (Page.Rectangle.Height - PageImage.Height) / 2
        // Image_y = Page.Rectangle.Top + Image_x
        // e.Graphics.DrawImage(PageImage, Image_x, Image_y)
        // End If
        // '绘制标题==================================================
        // If Page.Text > "" Then
        // If Page.Rectangle = Me.SelectedRectangle Then
        // TextRenderer.DrawText(e.Graphics, Page.Text, Me.MouseMovePageFont, New Rectangle(x + 40, y, Me.Width - x - 40, Me.PageHeight), Me.MouseMovePageForeColor, Flags)
        // Else
        // TextRenderer.DrawText(e.Graphics, Page.Text, Me.PageForeFont, New Rectangle(x + 40, y, Me.Width - x - 40, Me.PageHeight), Me.PageForeColor, Flags)
        // End If
        // End If
        // '绘制下拉箭头==============================================
        // Dim arrow As Image = My.Resources.TopicBar_arrow0
        // If Page.ExpandPage Then
        // arrow = My.Resources.TopicBar_arrow1
        // End If
        // Dim arrow_x As Integer = Page.Rectangle.Right - arrow.Height - (Page.Rectangle.Height - arrow.Height) / 2
        // Dim arrow_y As Integer = Page.Rectangle.Top + (Page.Rectangle.Height - arrow.Height) / 2
        // e.Graphics.DrawImage(arrow, arrow_x, arrow_y)
        // End If
        // ''绘制子项==================================================
        // If Page.ExpandPage = True Then
        // For Each Item As TopicBarItem In Page.Items
        // If Item.Visible = True Then
        // y = Me.Padding.Top + h - Me.VerticalScroll.Value
        // Item.Rectangle = New Rectangle(x, y, w, Me.ItemHeight)
        // h += Me.ItemHeight
        // If Item.Rectangle.Top < Me.Height Then
        // Points(0) = New Point(x, y)
        // Points(1) = New Point(w, Item.Rectangle.Top)
        // Points(2) = New Point(w, Item.Rectangle.Bottom)
        // Points(3) = New Point(x, Item.Rectangle.Bottom)
        // Using Brushes As New LinearGradientBrush(Item.Rectangle, Me.ItemTopColor, Me.ItemBottomColor, 90)
        // If Item Is Me.SelectedItem Then
        // Brushes.LinearColors = {Me.SelectedItemTopColor, Me.SelectedItemBottomColor}
        // ElseIf Item.Rectangle = Me.SelectedRectangle Then
        // Brushes.LinearColors = {Me.MouseMoveItemTopColor, Me.MouseMoveItemBottomColor}
        // End If
        // e.Graphics.FillPolygon(Brushes, Points)
        // End Using
        // '绘制分割线================================================
        // Using Pen As New Pen(Me.ItemSplit0Color)
        // e.Graphics.DrawLine(Pen, New Point(x, Item.Rectangle.Top), New Point(w, Item.Rectangle.Top))
        // Pen.Color = Me.ItemSplit1Color
        // e.Graphics.DrawLine(Pen, New Point(x, Item.Rectangle.Bottom), New Point(w, Item.Rectangle.Bottom))
        // End Using
        // '绘制图标==================================================
        // If Item.Image IsNot Nothing Then
        // Dim Image As System.Drawing.Image = Item.Image.GetThumbnailImage(16, 16, myCallback, IntPtr.Zero)
        // Dim _Image_x As Integer = (Page.Rectangle.Height - Image.Height) / 2
        // Dim _Image_y As Integer = (Item.Rectangle.Height - Image.Height) / 2 + Item.Rectangle.Top
        // e.Graphics.DrawImage(Image, _Image_x, _Image_y)
        // End If
        // '绘制标题==================================================
        // If Item.Text > "" Then
        // Dim Item_Font As Font = Me.ItemForeFont
        // Dim Item_ForeColor As Color = Me.ItemForeColor
        // If Item Is Me.SelectedItem Then
        // Item_Font = Me.SelectedItemFont
        // Item_ForeColor = Me.SelectedItemForeColor
        // ElseIf Item.Rectangle = Me.SelectedRectangle Then
        // Item_Font = Me.MouseMoveItemFont
        // Item_ForeColor = Me.MouseMoveItemForeColor
        // End If
        // TextRenderer.DrawText(e.Graphics, Item.Text, Item_Font, New Rectangle(x + 40, Item.Rectangle.Top, Me.Width - x - 64, Me.ItemHeight), Item_ForeColor, Flags)
        // End If
        // End If
        // End If
        // Next
        // End If
        // h += Me.PageSpacing
        // End If
        // Next
        // End If
        // If Me.DrawHeight = True AndAlso h <> Me.AutoScrollMinSize.Height Then
        // Me.AutoScrollMinSize = New Size(0, h)
        // End If
        // Me.DrawHeight = False
        // End Sub
        internal bool ThumbnailCallback()
        {
            return false;
        }

        /// <summary>
        /// 鼠标移动时促发
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.SelectedRectangle = default;
            if (e.Button == MouseButtons.None)
            {
                foreach (TopicBarPage Page in this.Pages)
                {
                    if (Page.Visible)
                    {
                        if (Page.Rectangle.Contains(e.Location))
                        {
                            this.SelectedRectangle = Page.Rectangle;
                            break;
                        }
                        else if (Page.ExpandPage)
                        {
                            foreach (TopicBarItem Item in Page.Items)
                            {
                                if (Item.Visible && Item.Rectangle.Contains(e.Location))
                                {
                                    this.SelectedRectangle = Item.Rectangle;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            this.Invalidate();
        }

        /// <summary>
        /// 鼠标点击时促发
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left)
            {
                this.DrawHeight = true;
                foreach (TopicBarPage Page in this.Pages)
                {
                    if (Page.Visible)
                    {
                        if (Page.Rectangle.Contains(e.Location) == true && this.AllowFold && Page.AllowFold)
                        {
                            if (Page.ExpandPage == false)
                            {
                                using var BeforeEnabledEventArgs = new TopicBarPageBeforeEnabledEventArgs(Page);
                                BeforeEnabled?.Invoke(this, BeforeEnabledEventArgs);
                                if (BeforeEnabledEventArgs.Cancel == false)
                                {
                                    Page.ExpandPage = true;
                                    using (var AfterEnabledEventArgs = new TopicBarPageAfterEnabledEventArgs(Page))
                                    {
                                        AfterEnabled?.Invoke(this, AfterEnabledEventArgs);
                                    }
                                }
                            }
                            else
                            {
                                Page.ExpandPage = !Page.ExpandPage;
                            }

                            return;
                        }

                        if (Page.ExpandPage)
                        {
                            foreach (TopicBarItem item in Page.Items)
                            {
                                if (item.Visible && item.Enabled && item.Rectangle.Contains(e.Location))
                                {
                                    this._SelectedItem = item;
                                    if (this.SelectedItem is object)
                                    {
                                        this.Invalidate(this.SelectedItem.Rectangle);
                                    }

                                    this._SelectedItem = item;
                                    this.Invalidate();
                                    using (var NavigateItemClickEventArgs = new TopicBarItemClickEventArgs(item))
                                    {
                                        ItemClicked?.Invoke(this, NavigateItemClickEventArgs);
                                    }

                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }

        public void RaiseClick(TopicBarItem item)
        {
            using (var NavigateItemClickEventArgs = new TopicBarItemClickEventArgs(item))
            {
                ItemClicked?.Invoke(this, NavigateItemClickEventArgs);
            }
        }

        /// <summary>
        /// 鼠标离开控件后促发
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.SelectedRectangle = default;
            this.Invalidate();
        }

        private void TopicBar_SizeChanged(object sender, EventArgs e)
        {
            this.DrawHeight = true;
        }
    }
}